package com.yy.blogdemo.utils;


import com.google.common.cache.Cache;
import com.google.common.cache.CacheBuilder;
import org.springframework.stereotype.Component;

import java.util.concurrent.TimeUnit;

@Component
public class BaseCache {

    //可以以事件缓存 也可以以功能做缓存 私有缓存 相当于domain中的构建数据 这是构建缓存
    private Cache<String,Object> tenMinuteCache = CacheBuilder.newBuilder()
            .initialCapacity(10)//初始内存
            .maximumSize(100)//最大缓存
            .concurrencyLevel(5)//线程数量
            .expireAfterWrite(10, TimeUnit.SECONDS)//过期时期 这个是按照写入时间
            //过期时间不能过长不然数据会一直不更新，改变了数据库，可能后面会有实时更新
            .recordStats()//缓存命中率
            .build();

    private Cache<String,Object> tenMinuteCacheonehour = CacheBuilder.newBuilder()
            .initialCapacity(10)//初始内存
            .maximumSize(100)//最大缓存
            .concurrencyLevel(5)//线程数量
            .expireAfterWrite(60, TimeUnit.MINUTES)//过期时期 这个是按照写入时间
            .recordStats()//缓存命中率
            .build();

    public   Cache<String, Object> getTenMinuteCacheonehour() {
        return tenMinuteCacheonehour;
    }

    public void setTenMinuteCacheonehour(Cache<String, Object> tenMinuteCacheonehour) {
        this.tenMinuteCacheonehour = tenMinuteCacheonehour;
    }

    //setter getter
    public Cache<String, Object> getTenMinuteCache() {
        return tenMinuteCache;
    }

    public void setTenMinuteCache(Cache<String, Object> tenMinuteCache) {
        this.tenMinuteCache = tenMinuteCache;
    }

}
